<?php
/**
 * 通过文章ID获取网站的文章
 *
 * 主要用户模板文件中设置的文章列表数据
 *
 * @param     void
 * @return    void
 **/
function get_site_article_by_ids($ids,$limit = 10,$order='publishtime desc') {
    if(!$ids) return array();

    $where = array(
        'id'        =>  array('in',$ids),
        'status'    =>  1,
        'publishtime'   =>  array('lt',NOW_TIME),
        'sid'       =>  C('sid')
    );

    $filed = 'id,title,content,cid,author,hotnum,cover,tags,publishtime';

    $data = M('Article')->where($where)->limit('0,'.$limit)->field($field)->order($order)->select();

    return $data ? $data : array();
}

function get_site_tags_by_ids($ids) {
    if(!$ids) return array();

    $data = M('SiteTags')
         ->join('tags on site_tags.tagid=tags.id')
         ->where("tags.status = 1 and site_tags.status = 1 and site_tags.tagid in (" . implode(',',$ids) . ") and site_tags.sid=" . C('sid'))
         ->field('tags.id,tags.nickname,site_tags.num')
         ->order('site_tags.num desc')
         ->select();

    return $data ? $data : array();
}

function site_template_cache($keys) {
    return site_cache($keys,'template');
}

function site_cache($keys,$type = 'site') {
    $host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : '';
    if(!$host) return null;

    if(!in_array($type,array('site','category','menu','category_tags','template','link','advertise'))) return null;

    $keys = $type != 'site' ? ($keys ? $type .'.'. $keys : $type) : $keys;

    static $siteInfo = null;
    if($siteInfo === null) {
        $siteInfo = D('Site','Mongo')->where(array('host'=>$host))->find();
        C($siteInfo);
    }
    if(!$siteInfo) return null;

    $keyArray = explode('.',$keys);
    $info = $siteInfo;
    foreach($keyArray as $key) {
        $info = isset($info[$key]) ? $info[$key] : null;
        if(!$info) break;
    }

    return $info;
}
?>
